package com.btb.adapter.contract;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Uint;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.generated.Uint8;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.gas.ContractGasProvider;

import com.btb.core.contract.BaseContractFunction;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;

/**
 *
 *   合约方法定义
 * @author hqbzl
 *
 */
@Slf4j
public class Payment extends BaseContractFunction {

	public static final String FUNC_GETBALANCE = "getBalance";
	public static final String FUNC_BUY = "buy";

	public Payment(String contractAddress, Web3j web3j, Credentials credentials) {
		super(contractAddress, web3j, credentials);
	}

	public RemoteFunctionCall<TransactionReceipt> buy(BigInteger weiValue, String _orderID,
			BigInteger _type,
			BigInteger _orderTotalPrice) {
		final Function function = new Function(FUNC_BUY,
				Arrays.asList(new Utf8String(_orderID),
						new Uint8(_type),
						new Uint256(_orderTotalPrice)),
				Collections.emptyList());
		return executeRemoteCallTransaction(function, weiValue);
	}

	public RemoteFunctionCall<TransactionReceipt> buyEx(BigInteger weiValue, String _orderID, BigInteger _type,
			BigInteger _orderTotalPrice) {
		Function function = new Function(FUNC_BUY,
				Arrays.asList(new Utf8String(_orderID), new Uint(_type), new Uint(_orderTotalPrice)),
				Collections.emptyList());
		return executeRemoteCallTransaction(function, weiValue);
	}

	public RemoteFunctionCall<TransactionReceipt> getBalance() {
		final Function function = new Function(FUNC_GETBALANCE, Arrays.asList(), Collections.emptyList());
		return executeRemoteCallTransaction(function);
	}

	public List<Type> getBalanceEx()  {
		final Function function = new Function(FUNC_GETBALANCE, Arrays.asList(),
				Arrays.asList(new TypeReference<Uint>() {
				}));
		return readOnlyFunction(function);
	}

}
